package com.ccgk.logistics.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.logistics.domain.Currency;
import com.ccgk.logistics.domain.Fee;
import com.ccgk.logistics.domain.Order;
import com.ccgk.logistics.domain.OrderFee;
import com.ccgk.logistics.service.BusinessException;
import com.ccgk.logistics.service.CurrencyService;
import com.ccgk.logistics.service.FeeService;
import com.ccgk.logistics.service.OrderFeeService;
import com.ccgk.logistics.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderFeeController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderFeeService orderFeeService;
	@Autowired
	private FeeService feeService;
	@Autowired
	private CurrencyService currencyService;
	
	@RequestMapping("/{orderId}/fee/list")
	public String listFee(@PathVariable("orderId")Integer orderId,Model model){
		Order order = orderService.getById(orderId);
		List<OrderFee> internalFeeList = orderFeeService.getInternalFeeList(orderId);
		List<OrderFee> externalFeeList = orderFeeService.getExternalFeeList(orderId);
		model.addAttribute("order", order);
		model.addAttribute("internalFeeList", internalFeeList);
		model.addAttribute("externalFeeList", externalFeeList);
		return "orderfee/list";
	}
	
	@RequestMapping("/{orderId}/fee/add")
	public String toAddOrderFee(@PathVariable("orderId")Integer orderId,Model model){
		List<Fee> fees = feeService.findAll();
		List<Currency> currencies = currencyService.findAll();
		Order order = orderService.getById(orderId);
		model.addAttribute("fees", fees);
		model.addAttribute("currencies", currencies);
		model.addAttribute("order", order);
		return "orderfee/add";
		
	}
	
	@RequestMapping(value="/{orderId}/fee/add",method=RequestMethod.POST)
	@ResponseBody
	public String addOrderFee(OrderFee orderFee,@PathVariable("orderId")Integer orderId,Model model){
		try{
			orderFeeService.add(orderFee);
		}catch(BusinessException ex){
			if(ex.getMessage().equals("费用重复")){
				return "{flag:1}";
			}
		}
		
		return "{flag:0}";
	}
	
	@ResponseBody
	@RequestMapping("/{orderId}/fee/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		orderFeeService.batchDelete(idIntArray);
		return "{success:true}";
	}
	
	@RequestMapping("/{orderId}/fee/{feeId}")
	public String toEdit(@PathVariable("orderId")Integer orderId,@PathVariable("feeId")Integer feeId,Model model){
		Order order = orderService.getById(orderId);
		OrderFee orderFee = orderFeeService.getById(feeId);
		List<Currency> currencies = currencyService.findAll();
		model.addAttribute("order",order);
		model.addAttribute("orderFee",orderFee);
		model.addAttribute("currencies", currencies);
		return "orderfee/edit";
	}
	
	@RequestMapping(value="/{orderId}/fee/{feeId}",method=RequestMethod.POST)
	@ResponseBody
	public String edit(OrderFee orderFee,Model model){
		orderFeeService.update(orderFee);
		return "{flag:0}";
	}
}
